import React, { Component } from 'react';
import { FormattedMessage } from 'umi/locale';
import { Menu, Divider } from 'antd';
import { connect } from 'dva';
import GridContent from '@/components/PageHeaderWrapper/GridContent';
import BaseView from './UserSettingConfig/base';
import ProhibitWords from './UserSettingConfig/prohibitWords';
import MailSetting from './UserSettingConfig/mailSetting';
import SafeSetting from './UserSettingConfig/safeSetting';
import styles from './style.less';
import { hasPermission } from '../../utils/uiUtils';
import StorageSetting from './UserSettingConfig/storageSetting';

const { Item } = Menu;

@connect(({ accountAndsettings }) => ({
  // currentUser: accountAndsettings.currentUser,
  currentUser: '',
}))
class Settings extends Component {
  main = undefined;

  constructor(props) {
    super(props);
    const menuMap = {
      base: <FormattedMessage id="accountandsettings.menuMap.basic" defaultMessage="基础设置" />,
      security: (
        <FormattedMessage id="accountandsettings.menuMap.security" defaultMessage="邮箱设置" />
      ),
      safe: <FormattedMessage id="accountandsettings.menuMap.safe" defaultMessage="安全设置" />,
      binding: (
        <FormattedMessage id="accountandsettings.menuMap.binding" defaultMessage="违禁词设置" />
      ),
      storage: (
        <FormattedMessage id="accountandsettings.menuMap.storage" defaultMessage="存储设置" />
      ),
    };
    this.state = {
      mode: 'inline',
      menuMap,
      selectKey: 'base',
    };
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'accountAndsettings/fetchCurrent',
    });
    window.addEventListener('resize', this.resize);
    this.resize();
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }

  getMenu = () => {
    const { menuMap } = this.state;
    const menuList = [];
    Object.keys(menuMap).forEach(item => {
      if (
        item === 'base' ||
        (item === 'security' && hasPermission('email:setting')) ||
        (item === 'safe' && hasPermission('safe:setting')) ||
        (item === 'sensitive' && hasPermission('sensitivewords:setting')) ||
        (item === 'storage' && hasPermission('storage:setting'))
      ) {
        menuList.push(<Item key={item}>{menuMap[item]}</Item>);
      }
    });
    return menuList;
  };

  getRightTitle = () => {
    const { selectKey, menuMap } = this.state;
    return menuMap[selectKey];
  };

  selectKey = key => {
    this.setState({
      selectKey: key,
    });
  };

  resize = () => {
    if (!this.main) {
      return;
    }

    requestAnimationFrame(() => {
      if (!this.main) {
        return;
      }

      let mode = 'inline';
      const { offsetWidth } = this.main;

      if (this.main.offsetWidth < 641 && offsetWidth > 400) {
        mode = 'horizontal';
      }

      if (window.innerWidth < 768 && offsetWidth > 400) {
        mode = 'horizontal';
      }

      this.setState({
        mode,
      });
    });
  };

  renderChildren = () => {
    const { selectKey } = this.state;

    switch (selectKey) {
      case 'base':
        return <BaseView />;

      case 'security':
        return <MailSetting {...this.props} />;

      case 'safe':
        return <SafeSetting {...this.props} />;

      case 'binding':
        return <ProhibitWords />;

      case 'storage':
        return <StorageSetting {...this.props} />;

      default:
        break;
    }

    return null;
  };

  render() {
    // const { currentUser } = this.props;
    //
    // if (!currentUser.userid) {
    //   return '';
    // }

    const { mode, selectKey } = this.state;
    return (
      <GridContent>
        <div
          className={styles.main}
          ref={ref => {
            if (ref) {
              this.main = ref;
            }
          }}
        >
          <div className={styles.leftMenu}>
            <Menu mode={mode} selectedKeys={[selectKey]} onClick={({ key }) => this.selectKey(key)}>
              {this.getMenu()}
            </Menu>
          </div>
          <div className={styles.right}>
            <div className={styles.title}>{this.getRightTitle()}</div>
            <Divider />
            {this.renderChildren()}
          </div>
        </div>
      </GridContent>
    );
  }
}

export default Settings;
